﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml;
using System.Drawing;
using System.Reflection;

namespace iPhoneUI.Shell
{
    public class PropertyConfig : Config
    {
        public PropertyConfig(XmlNode config)
            : base(config)
        {
        }

        public Select SelectStatement
        {
            get
            {
                XmlNode select = GetNode().SelectSingleNode("select");
                if (select != null)
                    return new Select(select);

                return null;
            }
        }

        public ListConfig ListConfig
        {
            get
            {
                XmlNode list = GetNode().SelectSingleNode("list");
                if (list != null)
                    return new ListConfig(list);

                return null;
            }
        }

        public ObjectConfig ObjectConfig
        {
            get
            {
                XmlNode o = GetNode().SelectSingleNode("object");
                if (o != null)
                    return new ObjectConfig(o);

                return null;
            }
        }

        public object GetValue(Type type)
        {
            // if there is a select clause use that to determine the return value
            Select select = SelectStatement;
            if (select != null)
                return select.Evaluate(GetNode(), type);

            // if there is a list clause use that to determine the return value
            ListConfig list = ListConfig;
            if (list != null)
                return list.Create();

            // if there is a list clause use that to determine the return value
            ObjectConfig o = ObjectConfig;
            if (o != null)
                return o.CreateInstance();

            // otherwise use the inner text and attempt to coerce to the correct type
            return GetNode().CoerceInnerText(type);
        }
    }
}
